package dao;

import java.util.Collection;

import model.AlbumBean;
import web.dao.Criteria;
import web.dao.GenericDao;

public class AlbumDao extends GenericDao<AlbumBean> {

	public AlbumDao() {
		super();
	}

	public AlbumBean getAlbumById(String id) {
		Collection<AlbumBean> result = findByProperty("id", id);
		if (result.isEmpty()) {
			return null;
		} else {
			return result.iterator().next();
		}
	}

	public AlbumBean getAlbumByCatalog(String catalog) {
		Collection<AlbumBean> result = findByProperty("catalog", catalog);
		if (result.isEmpty()) {
			return null;
		} else {
			return result.iterator().next();
		}
	}

	public Collection<AlbumBean> getAlbumLikeCatalog(String catalog) {
		Criteria criteria = Criteria.like("catalog", "%" + catalog + "%");
		Collection<AlbumBean> result = findByCriteria(criteria);
		return result;
	}

	public Collection<AlbumBean> getAlbumLikeTitle(String title) {
		Criteria criteria = Criteria.like("title", "%" + title + "%");
		Collection<AlbumBean> result = findByCriteria(criteria);
		return result;
	}

	@Override
	public AlbumBean find(AlbumBean album) {
		return getAlbumByCatalog(album.getCatalog());
	}

	public int remove(String catalog) {
		StringBuffer sql = new StringBuffer();
		sql.append("DELETE FROM ");
		sql.append(getTableName());
		sql.append(" WHERE catalog='");
		sql.append(catalog);
		sql.append(QUOTE);
		int result = executeUpdate(sql.toString());
		return result;
	}
}
